package org.example.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.Exception.RollBackException;
import org.example.dao.HelpDao;
import org.example.dao.HelpInfoDao;
import org.example.domain.Goods;
import org.example.domain.Help;
import org.example.domain.HelpInfo;
import org.example.domain.PageBean;
import org.example.domain.response.ResponseResult;
import org.example.services.HelpService;
import org.example.utils.PageConvertPageBeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Transactional
@Service
public class HelpServiceImpl implements HelpService {
    @Resource
    private HelpDao helpDao;
    @Resource
    private HelpInfoDao helpInfoDao;

//    新增帮助模块
    @Override
    public ResponseResult saveHelp(Help help) {
        try {
            helpDao.insert(help);
            log.info("新增帮助成功",help);
            return ResponseResult.success("新增帮助成功",help);
        }catch (Exception e){
            e.printStackTrace();
            log.info("新增帮助失败");
            throw new RollBackException("新增帮助失败");
        }
    }

//    修改帮助模块
    @Override
    public ResponseResult updateHelp(Help help) {
        Help helpRes = helpDao.selectById(help.getHelpId());
        if(helpRes==null){
            log.info("帮助修改：帮助基本信息查询失败",help);
            return ResponseResult.failed("无该帮助信息");
        }
        try {
            helpDao.updateById(help);
            log.info(helpRes.getHelpId()+"帮助修改成功");
            return ResponseResult.success(helpRes.getHelpId()+"帮助修改成功",null);
        }catch (Exception e){
            e.printStackTrace();
            log.warn("更新帮助失败");
            throw new RollBackException("更新帮助失败");
        }
    }

    @Override
    public ResponseResult deleteHelp(Integer helpId) {
        try {
            Help help = helpDao.selectById(helpId);
            if(help==null){
                log.info("帮助删除：帮助基本信息查询失败");
                return ResponseResult.failed("无该帮助，请刷新");
            }
            helpDao.deleteById(helpId);
            helpInfoDao.deleteHelpInfoByHelpId(helpId);
            log.info("帮助删除成功");
            return ResponseResult.success("帮助删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            log.warn("删除帮助失败");
            throw new RollBackException("删除帮助失败");
        }
    }

    @Override
    public ResponseResult selectHelpById(Integer helpId,int currentPage, int pageSize) {
        try {
            Help help = helpDao.selectById(helpId);
            if(help==null){
                log.info("帮助删除：帮助基本信息查询失败");
                return ResponseResult.failed("无该帮助，请刷新");
            }
            Page<HelpInfo> page = new Page<>(currentPage,pageSize);
            QueryWrapper<HelpInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("help_id",helpId);
            Page<HelpInfo> helpInfoPage = helpInfoDao.selectPage(page, wrapper);
            PageBean<HelpInfo> pageBean=PageConvertPageBeanUtil.convert(helpInfoPage);
            log.info("按帮助Id分页查询成功");
            return ResponseResult.success("分页查询Id成功",pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("按商品id分页查询失败");
            throw new RollBackException("新增问答失败");
        }
    }


    @Override
    public ResponseResult saveHelpInfo(HelpInfo helpInfo) {
        try{
            Help help = helpDao.selectById(helpInfo.getHelpId());
            if(help == null){
                log.info("帮助模块不存在");
                return ResponseResult.failed("帮助模块不存在，请刷新");
            }
            int i = helpInfoDao.insert(helpInfo);
            return ResponseResult.success("新增问答成功",helpInfo);
        }catch (Exception e){
            e.printStackTrace();
            log.warn("新增问答失败");
            throw new RollBackException("新增问答失败");
        }
    }

    @Override
    public ResponseResult updateHelpInfo(HelpInfo helpInfo) {
        try {
            HelpInfo helpInfo1 = helpInfoDao.selectById(helpInfo.getHelpInfoId());
            if(helpInfo1 == null){
                log.info("帮助模块不存在");
                return ResponseResult.failed("帮助模块不存在，请刷新");
            }
            helpInfoDao.updateById(helpInfo);
            return ResponseResult.success("问答修改成功",null);
        }catch (Exception e){
            e.printStackTrace();
            log.warn("更新问答失败");
            throw new RollBackException("更新问答失败");
        }
    }

    @Override
    public ResponseResult deleteHelpInfo(Integer helpInfoId) {
        try {
            HelpInfo helpInfo = helpInfoDao.selectById(helpInfoId);
            if(helpInfo == null){
                log.info("删除问答：问答不存在");
                return ResponseResult.failed("问答不存在，请刷新");
            }
            helpInfoDao.deleteById(helpInfoId);
            return ResponseResult.success("删除问答成功",null);
        }catch (Exception e){
            e.printStackTrace();
            log.warn("删除问答失败");
            throw new RollBackException("删除问答失败");
        }
    }

    @Override
    public ResponseResult selectHelpInfo(Integer helpInfoId, int pageSize, int currentPage) {
        try {
            Page<HelpInfo> page = new Page<>(currentPage,pageSize);
            QueryWrapper<HelpInfo> wrapper = new QueryWrapper<>();
            wrapper.like("help_info_id",helpInfoId);
            Page<HelpInfo> helpInfoPage = helpInfoDao.selectPage(page, wrapper);
            PageBean<HelpInfo> pageBean=PageConvertPageBeanUtil.convert(helpInfoPage);
            log.info("按分页查询成功");
            return ResponseResult.success("分页查询成功",pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("按分页查询失败");
            throw new RollBackException("新增问答失败");
        }
    }


    @Override
    public ResponseResult getHelpByPage(int currentPage, int pageSize) {
        Page<Help> page = new Page(currentPage, pageSize);
//        封装
        Page<Help> helpPage = helpDao.selectPage(page, null);
        PageBean<Help> pageBean = PageConvertPageBeanUtil.convert(helpPage);
        log.info("分页查询：成功{}",pageBean);
        return ResponseResult.success("分页查询成功",pageBean);
    }

    @Override
    public ResponseResult getHelpInfoByPage(int currentPage, int pageSize) {
        Page<HelpInfo> page = new Page(currentPage, pageSize);
//        封装
        Page<HelpInfo> helpInfoPage = helpInfoDao.selectPage(page, null);
        PageBean<HelpInfo> pageBean = PageConvertPageBeanUtil.convert(helpInfoPage);
        log.info("分页查询：成功{}",pageBean);
        return ResponseResult.success("分页查询成功",pageBean);
    }


}
